package com.h2cloud.T.Generic;

import java.util.ArrayList;
import java.util.List;

/**
 * @Package: com.h2cloud.T
 * @ClassName: GenericClassDemo1
 * @Author: h2cloud
 * @CreateTime: 2021/8/8 10:12 下午
 * @Description:
 */
public class _3_superTest {

    public static void main(String[] args) {

        List<? super Human> humanList = new ArrayList<>();

        // 只能指向Human及其父类型的List：灵长类、生物类
        humanList = new ArrayList<Primate>();
        humanList = new ArrayList<Creature>();
        // humanList = new ArrayList<Chinese>(); // ERROR

        // 可以存 human及其子类
        humanList.add(new Human("女性"));
        humanList.add(new Chinese("中国人"));
        // 存其他会报错
        // humanList.add(new Primate("灵长类动物")); // ERROR
        // humanList.add(new Creature("外星生物")); // ERROR
        // humanList.add("无关类型，比如String"); // ERROR

        // 取出时用 Object强转
        Object object = humanList.get(0);
    }


    static class Creature {
        public Creature(String name) {
            this.name = name;
        }
        private String name;
    }

    static class Primate extends Creature {
        public Primate(String name) {
            super(name);
        }
    }

    static class Human extends Primate {
        public Human(String name) {
            super(name);
        }
    }

    static class Chinese extends Human {
        public Chinese(String name) {
            super(name);
        }
    }

    static class Japanese extends Human {
        public Japanese(String name) {
            super(name);
        }
    }
}
